package com.atguigu.crowd.mvc.handler;

import com.atguigu.crowd.entity.Role;
import com.atguigu.crowd.service.api.RoleService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleHandler {
    @Autowired
    private RoleService roleService;

    @ResponseBody
    @RequestMapping("role/save.json")
        public ResultEntity<String> saveRole(Role role){
            roleService.saveRole(role);
            return ResultEntity.successWithoutData();
        }

    @ResponseBody
    @RequestMapping("role/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "pageNum" ,defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize" ,defaultValue = "1") Integer pageSize,
            @RequestParam(value = "keyword" ,defaultValue = "1") String keyword
    ){
//        如果有异常，交给异常处理机制来处理。在dispatchServlet中会有大的try catch
        PageInfo<Role> pageInfo = roleService.getPageInfo(pageNum, pageSize, keyword);
        return ResultEntity.successWithData(pageInfo);
    }

}
